﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;

using SGCG.Helpers;
using System.Diagnostics;

namespace SGCG.Model
{
    public class ReflectionGroup : GraphicObject
    {

        private readonly Line mirrorLine;
        public Line MirrorLine { get { return mirrorLine; } }

        public ReflectionGroup(GraphicObject parent, Line mirrorLine) 
            : base(parent) 
        {
            mirrorLine.PropertyChanged += (s, e) => RaiseGraphicPropertyChanged("Line");

            mirrorLine.Parent = this;
            this.mirrorLine = mirrorLine;
        }

        public override GraphicObject Attach(IDrawingCanvas drawingCanvas)
        {
            mirrorLine.Attach(drawingCanvas);
            return base.Attach(drawingCanvas);
        }

        private ObservableCollection<GraphicObject> mirroredObjects;
        public ObservableCollection<GraphicObject> MirroredObjects
        {
            get
            {
                if (mirroredObjects == null)
                {
                    mirroredObjects = new ObservableCollection<GraphicObject>();
                    mirroredObjects.CollectionChanged += 
                        (s, e) => RaiseGraphicPropertyChanged("MirroredObjects");
                }
                return mirroredObjects;
            }
            set
            {
                mirroredObjects = value;
                RaiseGraphicPropertyChanged("MirroredObjects");
            }
        }

        public override bool Selected
        {
            get { return base.Selected; }
            set
            {
                mirrorLine.Selected = value;
                base.Selected = value;
            }
        }

        private void FilterObjects()
        {
            List<GraphicObject> toRemove = new List<GraphicObject>();
            MirroredObjects.Each(g =>
                {
                    if (g.Deleted)
                        toRemove.Add(g);
                });
            foreach (var item in toRemove)
                MirroredObjects.Remove(item);
        }

        public override void Draw()
        {
            mirrorLine.Draw();
            Point A = new Point(MirrorLine.ComputedX1, MirrorLine.ComputedY1);
            Point B = new Point(MirrorLine.ComputedX2, MirrorLine.ComputedY2);
            ITransform transform = new MirrorTransform(A, B.Subtract(A).Normalized());
            FilterObjects();
            MirroredObjects.Each(g =>
                {
                    GraphicObject copy = g.CreateCopy();
                    copy.Selected = false;
                    copy.Attach(DrawingCanvas);
                    copy.CurrentTransforms.Add(transform);
                    copy.Draw();
                });
        }

        public override GraphicObject CreateCopy()
        {
            throw new NotImplementedException();
        }

        public override void ConsolidateTransform(ITransform transform)
        {
            mirrorLine.ConsolidateTransform(transform);
        }

    }
}
